//
//  GSDKUserAccountView.m
//  GameSDK
//
//  Created by Johnny on 2017/8/2.
//  Copyright © 2017年 Johnny. All rights reserved.
//

#import "GSDKUserAccountView.h"
#import "GSDKCommonMethod.h"
#import "GSDKUserConfig.h"
#import "YYKit.h"
@interface GSDKUserAccountView ()
@property (nonatomic,retain) UIImageView * logoImageView;
@property (nonatomic,retain) UILabel * unLabel;

@property (nonatomic,retain) UIButton * centerButton;
@property (nonatomic,retain) UIButton * comeButton;
@property (nonatomic,retain) UIButton * serviceButton;
@property (nonatomic,retain) UIButton * switchAccountButton;
@property (nonatomic,retain) UIButton * registeredButton;
@property (nonatomic,retain) UIButton * cancelButton;

@end

@implementation GSDKUserAccountView

- (void)dealloc {
    DEBUGMSG(@"GSDKUserAccountView dealloc");
}

- (id)init {
    self = [super init];
    if (self != nil) {
        [self setupUI];
    }
    return self;
}

- (void)reloadAt:(GSDKUserAccountViewModel *)viewModel {
    _unLabel.text = [NSString stringWithFormat:@"%@%@", [NSString localizedStringForKey:@"GSDK_isloggedin_text" value:@"已登录："], viewModel.userName];
}

#pragma mark - setupUI
- (void)setupUI {
    self.backgroundColor = [UIColor whiteColor];
    self.layer.cornerRadius = 4;
    
    _cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_cancelButton addTarget:self action:@selector(cancelAction) forControlEvents:UIControlEventTouchUpInside];
    [_cancelButton setImage:[UIImage imageNamed:@"ZYSDK_Resourcres.bundle/pic/close"] forState:UIControlStateNormal];
    _cancelButton.frame = CGRectMake(UserViewWidth - 40, 10, 30, 30);
    [self addSubview:_cancelButton];
    
    _logoImageView = [[UIImageView alloc]init];
    _logoImageView.image = [UIImage imageNamed:@"ZYSDK_Resourcres.bundle/pic/newLogo"];
    _logoImageView.frame = CGRectMake(UserViewWidth/2 - 119.7/2, 15, 119.7, 35);
    [self addSubview:_logoImageView];
    
    _unLabel = [[UILabel alloc]init];
    _unLabel.frame = CGRectMake(30, _logoImageView.frame.size.height + _logoImageView.frame.origin.y + 30, UserViewWidth - 30*2, 20);
    if (UserViewWidth < 320) {
        _unLabel.frame = CGRectMake(10, _logoImageView.frame.size.height + _logoImageView.frame.origin.y + 30, UserViewWidth - 10*2, 20);
    }
//    _unLabel.text = @"正在登录账号";
    _unLabel.textAlignment = NSTextAlignmentCenter;
    _unLabel.textColor = [UIColor colorWithHexString:@"#8d8c8c"];
    _unLabel.font = [UIFont systemFontOfSize:17];
    [self addSubview:_unLabel];
    
    
    _centerButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _centerButton.frame = CGRectMake(_unLabel.frame.origin.x, _unLabel.frame.origin.y + _unLabel.frame.size.height + 15, _unLabel.frame.size.width, 36);
    _centerButton.backgroundColor = [UIColor colorWithHexString:@"#f5f5f5"];
    _centerButton.titleLabel.font = [UIFont systemFontOfSize:14.5];
    [_centerButton setImage:[UIImage imageNamed:@"ZYSDK_Resourcres.bundle/pic/account"] forState:UIControlStateNormal];
    [_centerButton setTitleColor:[UIColor colorWithHexString:@"#525252"] forState:UIControlStateNormal];
    [_centerButton setTitle:[NSString stringWithFormat:@"  %@", [NSString localizedStringForKey:@"GSDK_usercenter_text" value:@"我的账号中心"]] forState:UIControlStateNormal];
    [_centerButton addTarget:self action:@selector(centerAction) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_centerButton];
    _centerButton.layer.cornerRadius = 2;
    
    _comeButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _comeButton.frame = CGRectMake(_unLabel.frame.origin.x, _centerButton.frame.origin.y + _centerButton.frame.size.height + 10, _unLabel.frame.size.width, 36);
    _comeButton.backgroundColor = [UIColor colorWithHexString:@"#0197d2"];
    _comeButton.titleLabel.font = [UIFont systemFontOfSize:14.5];
    [_comeButton setTitle: [NSString localizedStringForKey:@"GSDK_entergame_text" value:@"进入游戏"] forState:UIControlStateNormal];
    [_comeButton addTarget:self action:@selector(comeAction) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_comeButton];
    _comeButton.layer.cornerRadius = 2;
    
    NSString * switchAccountText = [NSString stringWithFormat:@" %@", [NSString localizedStringForKey:@"GSDK_switchaccount_text" value:@"切换账号"]];
    CGSize switchAccountSize = [switchAccountText sizeWithFont:[UIFont systemFontOfSize:13]
                               constrainedToSize:CGSizeMake(UserViewWidth, 24) lineBreakMode:NSLineBreakByWordWrapping];
    
    _switchAccountButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _switchAccountButton.frame = CGRectMake(UserViewWidth - (28 + switchAccountSize.width) - _unLabel.frame.origin.x, _comeButton.frame.origin.y + _comeButton.frame.size.height + 16, 28 + switchAccountSize.width, 24);
    _switchAccountButton.titleLabel.font = [UIFont systemFontOfSize:13];
    [_switchAccountButton setTitleColor:[UIColor colorWithHexString:@"#777777"] forState:UIControlStateNormal];
    [_switchAccountButton setImage:[UIImage imageNamed:@"ZYSDK_Resourcres.bundle/pic/switch"] forState:UIControlStateNormal];
    [_switchAccountButton addTarget:self action:@selector(switchAccountAction) forControlEvents:UIControlEventTouchUpInside];
    [_switchAccountButton setTitle:switchAccountText forState:UIControlStateNormal];
    [self addSubview:_switchAccountButton];
    
//    NSString * registeredText = [NSString stringWithFormat:@" %@", [NSString localizedStringForKey:@"GSDK_registeraccount_text" value:@"注册账号"]];
//    CGSize registeredSize = [registeredText sizeWithFont:[UIFont systemFontOfSize:13]
//                                             constrainedToSize:CGSizeMake(UserViewWidth, 24) lineBreakMode:NSLineBreakByWordWrapping];
//    
//    _registeredButton = [UIButton buttonWithType:UIButtonTypeCustom];
//    _registeredButton.frame = CGRectMake(_switchAccountButton.frame.origin.x - 10 - (28 + registeredSize.width), _switchAccountButton.frame.origin.y, 28 + registeredSize.width, 24);
//    _registeredButton.titleLabel.font = [UIFont systemFontOfSize:13];
//    [_registeredButton setTitleColor:[UIColor colorWithHexString:@"#777777"] forState:UIControlStateNormal];
//    [_registeredButton setImage:[UIImage imageNamed:@"ZYSDK_Resourcres.bundle/pic/regUser"] forState:UIControlStateNormal];
//    [_registeredButton addTarget:self action:@selector(registeredAction) forControlEvents:UIControlEventTouchUpInside];
//    [_registeredButton setTitle:registeredText forState:UIControlStateNormal];
//    [self addSubview:_registeredButton];
    
    /*
     _centerButton = [UIButton buttonWithType:UIButtonTypeCustom];
     _centerButton.frame = CGRectMake(_unLabel.frame.origin.x, _unLabel.frame.origin.y + _unLabel.frame.size.height + 15, _unLabel.frame.size.width, 36);
     _centerButton.backgroundColor = [UIColor colorWithHexString:@"#f5f5f5"];
     _centerButton.titleLabel.font = [UIFont systemFontOfSize:14.5];
     [_centerButton setImage:[UIImage imageNamed:@"ZYSDK_Resourcres.bundle/pic/account"] forState:UIControlStateNormal];
     [_centerButton setTitleColor:[UIColor colorWithHexString:@"#525252"] forState:UIControlStateNormal];
     [_centerButton setTitle:@" 我的账号中心" forState:UIControlStateNormal];
     [_centerButton addTarget:self action:@selector(centerAction) forControlEvents:UIControlEventTouchUpInside];
     [self addSubview:_centerButton];
     _centerButton.layer.cornerRadius = 4;
     
     _comeButton = [UIButton buttonWithType:UIButtonTypeCustom];
     _comeButton.frame = CGRectMake(_centerButton.frame.origin.x, _centerButton.frame.origin.y + _centerButton.frame.size.height + 10, _centerButton.frame.size.width, 36);
     _comeButton.backgroundColor = [UIColor colorWithHexString:@"#0197d2"];
     _comeButton.titleLabel.font = [UIFont systemFontOfSize:14.5];
     [_comeButton setTitle:@"进入游戏" forState:UIControlStateNormal];
     [_comeButton addTarget:self action:@selector(comeAction) forControlEvents:UIControlEventTouchUpInside];
     [self addSubview:_comeButton];
     _comeButton.layer.cornerRadius = 4;
     
     _serviceButton = [UIButton buttonWithType:UIButtonTypeCustom];
     _serviceButton.backgroundColor = [UIColor colorWithHexString:@"#6dc672"];
     _serviceButton.frame = CGRectMake(_comeButton.frame.origin.x, _comeButton.frame.origin.y + _comeButton.frame.size.height + 10, 170, 28);
     _serviceButton.titleLabel.font = [UIFont systemFontOfSize:13];
     [_serviceButton addTarget:self action:@selector(serviceAction) forControlEvents:UIControlEventTouchUpInside];
     [_serviceButton setImage:[UIImage imageNamed:@"ZYSDK_Resourcres.bundle/pic/icon_service"] forState:UIControlStateNormal];
     [_serviceButton setTitle:@" 客服 : 400-618-6565" forState:UIControlStateNormal];
     [self addSubview:_serviceButton];
     _serviceButton.layer.cornerRadius = 14;
     
     _switchAccountButton = [UIButton buttonWithType:UIButtonTypeCustom];
     _switchAccountButton.frame = CGRectMake(UserViewWidth - 80 - 30, _serviceButton.frame.origin.y + _serviceButton.frame.size.height + 5, 80, 24);
     _switchAccountButton.titleLabel.font = [UIFont systemFontOfSize:13];
     [_switchAccountButton setTitleColor:[UIColor colorWithHexString:@"#777777"] forState:UIControlStateNormal];
     [_switchAccountButton setImage:[UIImage imageNamed:@"ZYSDK_Resourcres.bundle/pic/switch"] forState:UIControlStateNormal];
     [_switchAccountButton addTarget:self action:@selector(switchAccountAction) forControlEvents:UIControlEventTouchUpInside];
     [_switchAccountButton setTitle:@" 切换账号" forState:UIControlStateNormal];
     [self addSubview:_switchAccountButton];
     
     _registeredButton = [UIButton buttonWithType:UIButtonTypeCustom];
     _registeredButton.frame = CGRectMake(_switchAccountButton.frame.origin.x - 10 - 80, _switchAccountButton.frame.origin.y, 80, 24);
     _registeredButton.titleLabel.font = [UIFont systemFontOfSize:13];
     [_registeredButton setTitleColor:[UIColor colorWithHexString:@"#777777"] forState:UIControlStateNormal];
     [_registeredButton setImage:[UIImage imageNamed:@"ZYSDK_Resourcres.bundle/pic/regUser"] forState:UIControlStateNormal];
     [_registeredButton addTarget:self action:@selector(registeredAction) forControlEvents:UIControlEventTouchUpInside];
     [_registeredButton setTitle:@" 注册账号" forState:UIControlStateNormal];
     [self addSubview:_registeredButton];
     */
}

#pragma mark - Action
- (void)cancelAction {
    [[NSNotificationCenter defaultCenter] postNotificationName:GSDKUserCancelNoticationName object:nil];
}
- (void)centerAction {
    if (_centerBlock) {
        _centerBlock();
    }
}

- (void)comeAction {
    if (_comeBlock) {
        _comeBlock();
    }
}

- (void)serviceAction {
    
    UIWebView * webView = [[UIWebView alloc]init];
    NSURL *url = [NSURL URLWithString:@"tel://400-618-6565"];
    [webView loadRequest:[NSURLRequest requestWithURL:url]];
    [self addSubview:webView];
}

- (void)switchAccountAction {
    if (_switchAccountBlock) {
        _switchAccountBlock();
    }
}

- (void)registeredAction {
    if (_registeredBlock) {
        _registeredBlock();
    }
}

@end
